<template>
  <div>
    <el-card>
      <template #header>
        <div>班级作业列表</div>
      </template>
      <el-table v-loading="loading" :data="assignments" stripe>
        <el-table-column label="作业标题" prop="title" />
        <el-table-column label="截止日期" prop="deadline">
          <template #default="scope">{{ new Date(scope.row.deadline).toLocaleString() }}</template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button size="small" type="primary" @click="viewSubmissions(scope.row)"
              >查看提交</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { apiClient, handleErrorAndRedirect } from '@/api'

const route = useRoute()
const router = useRouter()
const classId = route.params.classId as string

const assignments = ref<any[]>([])
const loading = ref(false)

const fetchAssignments = async () => {
  loading.value = true
  try {
    const res = await apiClient.get(`/assignments/class/${classId}/submission`)
    assignments.value = res.data
  } catch (error) {
    handleErrorAndRedirect(error, '获取作业列表失败')
  } finally {
    loading.value = false
  }
}

const viewSubmissions = (assignment) => {
  router.push({ name: 'AssignmentSubmissions', params: { assignmentId: assignment.assignmentId } })
}

onMounted(fetchAssignments)
</script>
